﻿// Decompiled with JetBrains decompiler
// Type: StoryMode.Quests.QuestTasks.RecruitTroopTutorialQuestTask
// Assembly: StoryMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 11BAE65F-6C15-4628-A9C6-1B968588CDA1
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\StoryMode\bin\Win64_Shipping_Client\StoryMode.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.SaveSystem;

#nullable disable
namespace StoryMode.Quests.QuestTasks
{
  public class RecruitTroopTutorialQuestTask : QuestTaskBase
  {
    private int _targetRecruitAmount;
    private Func<CharacterObject, bool> _recruitTypeConditions;
    private Settlement _recruitSettlement;
    [SaveableField(1)]
    private readonly JournalLog _progressLog;
    [SaveableField(2)]
    private int _recruitedTroopAmount;

    public RecruitTroopTutorialQuestTask(
      Action onSucceed,
      int targetRecruitAmount,
      Func<CharacterObject, bool> recruitTypeConditions,
      Settlement recruitSettlement = null,
      JournalLog progressLog = null)
      : base(onSucceedAction: onSucceed)
    {
      this._targetRecruitAmount = targetRecruitAmount;
      this._recruitTypeConditions = recruitTypeConditions;
      this._recruitSettlement = recruitSettlement;
      this._progressLog = progressLog;
      this._recruitedTroopAmount = 0;
    }

    public void InitializeTaskOnLoad(
      int targetRecruitAmount,
      Func<CharacterObject, bool> recruitTypeConditions,
      Settlement recruitSettlement = null)
    {
      this._targetRecruitAmount = targetRecruitAmount;
      this._recruitTypeConditions = recruitTypeConditions;
      this._recruitSettlement = recruitSettlement;
    }

    public override void SetReferences()
    {
      CampaignEvents.OnUnitRecruitedEvent.AddNonSerializedListener((object) this, new Action<CharacterObject, int>(this.OnUnitRecruited));
    }

    private void OnUnitRecruited(CharacterObject character, int amount)
    {
      if (!this.IsActive || this._recruitSettlement != null && Settlement.CurrentSettlement != this._recruitSettlement || !this._recruitTypeConditions(character))
        return;
      this._recruitedTroopAmount += amount;
      if (this._recruitedTroopAmount >= this._targetRecruitAmount)
      {
        this._progressLog?.UpdateCurrentProgress(this._targetRecruitAmount);
        this.Finish(QuestTaskBase.FinishStates.Success);
      }
      else
        this._progressLog?.UpdateCurrentProgress(this._recruitedTroopAmount);
    }

    internal static void AutoGeneratedStaticCollectObjectsRecruitTroopTutorialQuestTask(
      object o,
      List<object> collectedObjects)
    {
      ((QuestTaskBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._progressLog);
    }

    internal static object AutoGeneratedGetMemberValue_progressLog(object o)
    {
      return (object) ((RecruitTroopTutorialQuestTask) o)._progressLog;
    }

    internal static object AutoGeneratedGetMemberValue_recruitedTroopAmount(object o)
    {
      return (object) ((RecruitTroopTutorialQuestTask) o)._recruitedTroopAmount;
    }
  }
}
